package callgraph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class Main {
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// CommandLine
		int i = 0;
		String arg = null;
		String inputFileString = "merge_"+((int)(Math.random()*100000));
		String folder = null;
		String destination = "D:\\merge\\";
		String graphName = "Interactive_SHELL_graph";
		String type = "lg";
		String file = "convert";
		boolean debugMode = false;

		while (i < args.length) {
			arg = args[i];

			// use this to start the debugmode
			if (arg.equalsIgnoreCase("-debug")) {
				debugMode = true;
				System.out.println("DebugMode: ON");
			}

			// With the commandline parameter '-inputFile '$filepath'' you can
			// describe where the serialized file is located
			else if (arg.equalsIgnoreCase("-inputFile")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					inputFileString = args[i + 1];

				} else
					System.out.println("-inputFile requires a filename (e.g. -inputFile \"$filePath\")");
			} else if (arg.equalsIgnoreCase("-dest")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					destination = args[i + 1];

				} else
					System.out.println("-dest requires a filename (e.g. -dest \"$filePath\")");
			} else if (arg.equalsIgnoreCase("-graphName")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					graphName = args[i + 1];

				} else
					System.out.println("-inputFile requires a filename (e.g. -graphName \"$name\")");
				// With the commandline parameter '-graphType '$type'' you
				// can whether a .lg or .pairs file should be created
			} else if (arg.equalsIgnoreCase("-graphType")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					if (!type.equalsIgnoreCase("lg") && !type.equalsIgnoreCase("pairs")) {
						System.out.println("-graphType must have a type (e.g. -type {lg|pairs})");
					} else
						type = args[i + 1];
				} else
					System.out.println("-graphType requires a filename (e.g. -filename)");
			} else if (arg.equalsIgnoreCase("-dest")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					destination = args[i + 1];

				} else
					System.out.println("-dest requires a filename (e.g. -dest \"$filePath\")");
			} else if (arg.equalsIgnoreCase("-folder")) {
				if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
					folder = args[i + 1];
					destination = moveUpFolder(args[i+1],1);
				} else
					System.out.println("-folder requires a filename (e.g. -dest \"$filePath\")");
			}
			i++;
		}
		
		//merge all files in "folder" into an AdjacenceList
		AdjacenceList graphInput = new GraphReader().mergeFiles(folder);
	
		GraphWriter converter = new GraphWriter(graphInput, destination + inputFileString, graphName, type);
		System.out.println("Output Directory: "+destination + inputFileString);
		
		if (type.equalsIgnoreCase("wg2")){
			converter.writeR_w_total2wg2(true,true,true,true);
		}
		if (type.equalsIgnoreCase("pairs")){
			converter.writeR_w_total2pairs(false, true, true, true);
		}
		if (type.equalsIgnoreCase("lg"))
			converter.writeR_w_total2lg(true, true, true, true);
	}
	/**
	 * 
	 * @param path The path that should be manipulated
	 * @param levels The number of folders that should be moved up
	 * @return the manipulated path as a String
	 */
	public static String moveUpFolder(String path, int levels){
		String newPath = new String();
		newPath = path.substring(0, path.lastIndexOf("\\"));
		return newPath;
	}
}
